package com.qch.springboot_redis;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.util.HashSet;

/**
 * @Author qch
 * @Date 2021/5/8
 */
public class JedisClusterTest {
    public static void main(String[] args) throws IOException {
        JedisPoolConfig config = new JedisPoolConfig();
        HashSet<HostAndPort> jedisClusterNodes = new HashSet<>();

        //添加集群节点
        jedisClusterNodes.add(new HostAndPort("192.168.142.135",7001));
        jedisClusterNodes.add(new HostAndPort("192.168.142.135",7002));
        jedisClusterNodes.add(new HostAndPort("192.168.142.135",7003));
        jedisClusterNodes.add(new HostAndPort("192.168.142.135",7004));
        jedisClusterNodes.add(new HostAndPort("192.168.142.135",7005));
        jedisClusterNodes.add(new HostAndPort("192.168.142.135",7006));
       // jedisClusterNodes.add(new HostAndPort("192.168.142.135",7007));
        //jedisClusterNodes.add(new HostAndPort("192.168.142.135",7008));

        //获取集群连接
        JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes, config);

        //设置值
        jedisCluster.set("name:004","zhangfei4");

        //获取值
        String value = jedisCluster.get("name:004");
        System.out.println(value);

        //关闭连接
        jedisCluster.close();
    }
}
